package com.intellagric.service.traceability.Impl.trace_record;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.intellagric.common.pojo.LayuiDataGridResult;
import com.intellagric.common.pojo.ResponseMessage;
import com.intellagric.dao.traceability.RecordCheckDao;
import com.intellagric.mapper.TraceRecordCheckMapper;
import com.intellagric.module.traceability.CheckRecordVo;
import com.intellagric.pojo.TraceRecordCheck;
import com.intellagric.pojo.TraceRecordCheckExample;
import com.intellagric.service.traceability.trace_record.CheckRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: wl
 * @email: 844406720@qq.com
 * @Date: 2018/11/07
 * @DESC： 检查记录服务实现
 */
@Service
public class CheckRecordServiceImpl implements CheckRecordService {

    @Autowired
    private TraceRecordCheckMapper traceRecordCheckMapper;

    @Autowired
    private RecordCheckDao recordCheckDao;

    /**
     * 添加检查记录
     *
     * @param traceRecordCheck
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage addCheckRecord(TraceRecordCheck traceRecordCheck) {
        if (traceRecordCheckMapper.insertSelective(traceRecordCheck) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }    }

    /**
     * 删除检查记录
     * @param id
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage deleteCheckRecord(String id) {
        if (traceRecordCheckMapper.deleteByPrimaryKey(id) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 修改检查记录
     * @param traceRecordCheck
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage editCheckRecord(TraceRecordCheck traceRecordCheck) {
        if (traceRecordCheckMapper.updateByPrimaryKeySelective(traceRecordCheck) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 查询检查记录信息
     * @param id
     * @return TraceRecordCheck
     */
    @Override
    public TraceRecordCheck getCheckRecordById(String id) {
        return traceRecordCheckMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询检查记录信息列表
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult getCheckRecordList() {
        TraceRecordCheckExample traceRecordCheckExample = new TraceRecordCheckExample();
        traceRecordCheckExample.createCriteria().andIdIsNotNull();
        List<TraceRecordCheck> traceRecordCheckList = traceRecordCheckMapper.selectByExample(traceRecordCheckExample);
        return LayuiDataGridResult.success().add(traceRecordCheckList,traceRecordCheckList.size());
    }

    /**
     * 分页查询检查记录信息列表
     * @param page
     * @param rows
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult getCheckRecordPage(int page, int rows) {
        PageHelper.startPage(page,rows);
        TraceRecordCheckExample traceRecordCheckExample = new TraceRecordCheckExample();
        traceRecordCheckExample.createCriteria().andIdIsNotNull();
        List<TraceRecordCheck> traceRecordCheckList = traceRecordCheckMapper.selectByExample(traceRecordCheckExample);
        return LayuiDataGridResult.success().add(traceRecordCheckList,this.getCheckRecordList().getCount());
    }

    /**
     * 分页查询检查记录信息列表
     * @param page
     * @param rows
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult getCheckRecordPageByVo(int page, int rows, CheckRecordVo checkRecordVo) {
        PageHelper.startPage(page,rows);
        TraceRecordCheckExample traceRecordCheckExample = new TraceRecordCheckExample();
        traceRecordCheckExample.createCriteria().andIdIsNotNull();
        List<TraceRecordCheck> traceRecordCheckList = recordCheckDao.selectCheckRecordPageByVo(checkRecordVo);
        PageInfo<TraceRecordCheck> pageInfo =  new PageInfo<>(traceRecordCheckList);
        return LayuiDataGridResult.success().add(traceRecordCheckList,pageInfo.getTotal());
    }
}
